# -*- coding: utf-8 -*-
# @Time    : 2025/5/11 20:16
# @Author  : cx
# @File    : wei_yi_Y.py
# @Software: PyCharm
import uuid
# 生成 UUID 并转换为 32 位字符串（不含连字符）
device_ma = uuid.uuid4().hex  # 例如：'550e8400e29b41d4a7164466554400'

print(device_ma)

import time
import secrets
# 时间戳 + 随机数生成唯一值
timestamp = int(time.time() * 1000)
timestamp_hex = f"{timestamp:x}".zfill(12)  # 固定为12位，不足补零
random_part = secrets.token_hex(10)
device_ma = f"{timestamp_hex}{random_part}"[:32]  # 拼接后长度必为32位
print(device_ma)

device_ma = secrets.token_hex(16)  # 16字节 = 32位十六进制
print(device_ma)

# ffd7 e0a1 ce46 cc0a 0648 d195 ce7c aee8

""" 重试机制
import uuid
import asyncio
from typing import Optional

async def generate_unique_device_ma(db) -> str:
    max_attempts = 3
    for _ in range(max_attempts):
        device_ma = uuid.uuid4().hex
        try:
            # 执行插入操作，若冲突则抛出异常
            await db.execute(
                "INSERT INTO devices (device_ma) VALUES (?)", (device_ma,)
            )
            return device_ma
        except IntegrityError:  # 捕获唯一性冲突错误
            await asyncio.sleep(0.1)  # 短暂等待后重试
    raise ValueError("无法生成唯一的 device_ma")
"""

import asyncio  #协程异步
from mysql_database_utils_Y import AsyncMYSQL_L
from sql_yu_ju import SQL_YU_JI_l

sql = """
            INSERT INTO device_ma(device_ma) VALUES (%s)
        """
async def wei(db):
    #await db.execute(sql, device_ma)
    for i in range(30):
        device_mar = uuid.uuid4().hex
        sql_operations = [(sql, device_mar)]
        pd = await db.execute_operate(sql_operations)
        if pd:
            print("ok")
            print(i)
        else:
            print("fail")


async def tj():
    db = AsyncMYSQL_L('192.168.1.88', 3306, 'a18222970', 'cx123100', 'zhi_neng_wang_guan')
    await db.create_pool()  # 建立MYSQL连接池
    await wei(db)



    await db.close_pool()  # 断开连接池(销毁连接池)

if __name__ == "__main__":
    #asyncio.run(tj())